package com.leyou.item.controller;

import com.leyou.common.pojo.PageResult;
import com.leyou.common.pojo.ResultInfo;
import com.leyou.item.pojo.Brand;
import com.leyou.item.service.IBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 品牌信息
 */
@Controller
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private IBrandService brandService;

    /**
     * 查询品牌列表  分页
     * @param key      搜索条件
     * @param page     当前页
     * @param rows     每显示的条数
     * @param sortBy   排序字段
     * @param desc     是否降序
     * @return
     */
    @RequestMapping("/page")
    @ResponseBody
    public PageResult<Brand> list(@RequestParam(value = "key" ,required = false)String key,
                                  @RequestParam(value = "page",defaultValue = "1")Integer page,
                                  @RequestParam(value = "rows",defaultValue = "5")Integer rows,
                                  @RequestParam(value = "sortBy",required = false)String sortBy,
                                  @RequestParam(value = "desc",required = false)Boolean desc) throws Exception {

        return this.brandService.findBrandByPage(key,page,rows,sortBy,desc);
    }

    @PostMapping
    @ResponseBody
    public ResultInfo s1aveBrand(Brand brand, @RequestParam("cids") List<Long> cids) throws Exception {
        this.brandService.saveBrandAndCategoryBrand(brand,cids);
        return ResultInfo.success();
    }
}
